//
//  OrderUnfinishedTableViewCell.m
//  JinYong_iOS_Store
//
//  Created by 王厚一 on 2017/10/24.
//  Copyright © 2017年 why. All rights reserved.
//

#import "OrderUnfinishedTableViewCell.h"
#import "GoodsRowsTableViewCell.h"

@interface OrderUnfinishedTableViewCell ()  <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic,strong) UIView *backView;//整体背景视图

@property (nonatomic,strong) UITableView *tableView;//整个商品列表视图

@property (nonatomic,strong) UIView *bottomView;//底部视图

@property (nonatomic,strong) UILabel *countLabel;//订单索引
@property (nonatomic,strong) UILabel *statusLabel;//订单状态
@property (nonatomic,strong) UILabel *name;//名字
@property (nonatomic,strong) UILabel *buyCountLabel;//第几次下单
@property (nonatomic,strong) UILabel *addressLabel;//地址

@property (nonatomic,strong) UILabel *goodsCount;//商品数量

@property (nonatomic,strong) UIView *remarkView;//备注视图
@property (nonatomic,strong) UILabel *remarkLabel;//备注

@property (nonatomic,strong) UILabel *receivePrice;//收入金额
@property (nonatomic,strong) UILabel *orderTime;//订单时间
@property (nonatomic,strong) UILabel *orderNum;//订单号

@property (nonatomic,strong) NSMutableArray *dataSource;//商品数据源

@property (nonatomic,strong) UIButton *arriveBtn;//送达按钮
@property (nonatomic,strong) UIButton *cancelBtn;//取消按钮

@end

@implementation OrderUnfinishedTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor clearColor];
        [self createView];
    }
    return self;
}

#pragma mark - -------------------------视图部分----------------------------
- (void)createView {
    self.backView = ({
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(ActualW(10), ActualH(9.5), ActualW(355), ActualH(492.5))];
        view.backgroundColor = RGB(250, 250, 250);
        view.layer.cornerRadius = ActualW(2);
        view.layer.shadowColor = RGB(153, 153, 153).CGColor;//阴影
        view.layer.shadowOpacity = 0.8f;
        view.layer.shadowRadius = ActualW(2);
        view.layer.shadowOffset = CGSizeMake(0,0);
        view;
    });
    [self.contentView addSubview:self.backView];
    
    //订单索引
    self.countLabel = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(15), ActualH(7), ActualW(200), ActualH(16))];
        label.textColor = RGB(64, 137, 248);
        label.textAlignment = NSTextAlignmentLeft;
        label.font = FontX(FONTSIZE(15));
        label;
    });
    [self.backView addSubview:self.countLabel];
    
    //订单状态
    self.statusLabel = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(260), ActualH(8), ActualW(80), ActualH(13))];
        label.textColor = RGB(102, 102, 102);
        label.textAlignment = NSTextAlignmentRight;
        label.font = FontX(FONTSIZE(12));
        label;
    });
    [self.backView addSubview:self.statusLabel];
    
    UIView * topView = ({
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(30), ActualW(355), ActualH(72))];
        view.backgroundColor = RGB(255, 255, 255);
        view;
    });
    [self.backView addSubview:topView];
    //名字
    self.name = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(15), ActualH(10), ActualW(200), ActualH(13))];
        label.textColor = RGB(51, 51, 51);
        label.textAlignment = NSTextAlignmentLeft;
        label.font = FontX(FONTSIZE(12));
        label;
    });
    [topView addSubview:self.name];
    //下单次数
    self.buyCountLabel = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(15), ActualH(28.5), ActualW(100), ActualH(11))];
        label.textColor = RGB(153, 153, 153);
        label.textAlignment = NSTextAlignmentLeft;
        label.font = FontX(FONTSIZE(10));
        label;
    });
    [topView addSubview:self.buyCountLabel];
    //地址
    self.addressLabel = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(15), ActualH(45.5), ActualW(300), ActualH(13))];
        label.textColor = RGB(102, 102, 102);
        label.textAlignment = NSTextAlignmentLeft;
        label.font = FontX(FONTSIZE(12));
        label;
    });
    [topView addSubview:self.addressLabel];
    //定位图片和按钮
    UIImageView * locationImage = ({
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(ActualW(270.5), ActualH(9), ActualW(27), ActualH(27))];
        imageView.image = [UIImage imageNamed:@"dingwei"];
        imageView;
    });
    //    [topView addSubview:locationImage];
    
    UIButton * locationBtn = ({
        UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(ActualW(270.5), ActualH(9), ActualW(27), ActualH(27))];
        [button addTarget:self action:@selector(locationAction) forControlEvents:UIControlEventTouchUpInside];
        button;
    });
    //    [topView addSubview:locationBtn];
    
    //电话图片和按钮
    UIImageView * mobileImage = ({
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(ActualW(313), ActualH(9), ActualW(27), ActualH(27))];
        imageView.image = [UIImage imageNamed:@"dianhua"];
        imageView;
    });
    [topView addSubview:mobileImage];
    
    UIButton * mobileBtn = ({
        UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(ActualW(313), ActualH(9), ActualW(27), ActualH(27))];
        [button addTarget:self action:@selector(mobileAction) forControlEvents:UIControlEventTouchUpInside];
        button;
    });
    [topView addSubview:mobileBtn];
    
    //分割线
    UIView * breakLine = ({
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(ActualW(15), ActualH(102), ActualW(325), ActualH(0.5))];
        view.backgroundColor = RGB(238, 238, 238);
        view;
    });
    [self.backView addSubview:breakLine];
    UIView * breakView = ({
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(103), ActualW(355), ActualH(78))];
        view.backgroundColor = RGB(255, 255, 255);
        view;
    });
    [self.backView addSubview:breakView];
    //商品数量
    self.goodsCount = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(15), ActualH(112.5), ActualW(100), ActualH(13))];
        label.textColor = RGB(51, 51, 51);
        label.textAlignment = NSTextAlignmentLeft;
        label.font = FontX(FONTSIZE(12));
        label;
    });
    [self.backView addSubview:self.goodsCount];
    
    self.remarkView = ({
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(ActualW(15), ActualH(133.5), ActualW(325), ActualH(61))];
        view.backgroundColor = RGB(238, 238, 238);
        view;
    });
    [self.backView addSubview:self.remarkView];
    
    UILabel * subRemarkLabel = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(5), ActualH(5), ActualW(40), ActualH(13))];
        label.textColor = RGB(255, 156, 0);
        label.textAlignment = NSTextAlignmentLeft;
        label.text = @"备注：";
        label.font = FontX(FONTSIZE(12));
        label;
    });
    [self.remarkView addSubview:subRemarkLabel];
    
    self.remarkLabel = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(45), ActualH(5), ActualW(275), ActualH(51))];
        label.textColor = RGB(102, 102, 102);
        label.textAlignment = NSTextAlignmentLeft;
        label.numberOfLines = 0;
        label;
    });
    [self.remarkView addSubview:self.remarkLabel];
    
    //商品列表
    self.tableView = ({
        UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(197), ActualW(355), ActualH(175)) style:UITableViewStylePlain];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.backgroundColor = RGB(255, 255, 255);
        tableView;
    });
    [self.backView addSubview:self.tableView];
    
    //订单数量下方视图
    self.bottomView= ({
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(414.5), ActualW(355), ActualH(78))];
        view.backgroundColor = [UIColor clearColor];
        view;
    });
    [self.backView addSubview:self.bottomView];
    
    UIView * receiveView = ({
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(0), ActualW(355), ActualH(33))];
        view.backgroundColor = RGBA(255, 255, 255, 0.9);
        view;
    });
    [self.bottomView addSubview:receiveView];
    
    UIView * breakLine2 = ({
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(0), ActualW(355), ActualH(1))];
        view.backgroundColor = RGB(239, 239, 239);
        view;
    });
    [receiveView addSubview:breakLine2];
    
    UILabel * receiveLabel = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(15), ActualH(10.5), ActualW(100), ActualH(13))];
        label.textColor = RGB(51, 51, 51);
        label.text = @"本单收入";
        label.font = FontX(FONTSIZE(12));
        label;
    });
    [receiveView addSubview:receiveLabel];
    
    self.receivePrice = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(240), ActualH(10.5), ActualW(105), ActualH(13))];
        label.textColor = RGB(255, 139, 1);
        label.textAlignment = NSTextAlignmentRight;
        label.font = BIGFontX(FONTSIZE(12));
        label;
    });
    [receiveView addSubview:self.receivePrice];
    
    self.orderTime = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(15), ActualH(43), ActualW(300), ActualH(11))];
        label.textColor = RGB(153, 153, 153);
        label.font = FontX(FONTSIZE(10));
        label;
    });
    [self.bottomView addSubview:self.orderTime];
    
    self.orderNum = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(15), ActualH(59), ActualW(300), ActualH(11))];
        label.textColor = RGB(153, 153, 153);
        label.font = FontX(FONTSIZE(10));
        label;
    });
    [self.bottomView addSubview:self.orderNum];
    
    //确认取消按钮
    self.cancelBtn = ({
        UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(ActualW(237), ActualH(40), ActualW(44), ActualH(35))];
        [button addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
        button;
    });
    self.cancelBtn.hidden = YES;
    //    [self.bottomView addSubview:self.cancelBtn];
    
    UILabel * cancelLabel = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(22), ActualW(44), ActualH(13))];
        label.textColor = RGB(153, 153, 153);
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"取消订单";
        label.font = FontX(FONTSIZE(10));
        label;
    });
    
    [self.cancelBtn addSubview:cancelLabel];
    
    UIImageView * cancelImage = ({
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(ActualW(14), ActualH(0), ActualW(17), ActualH(17))];
        imageView.image = [UIImage imageNamed:@"cancel"];
        imageView;
    });
    [self.cancelBtn addSubview:cancelImage];
    
    self.arriveBtn = ({
        UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(ActualW(301), ActualH(40), ActualW(44), ActualH(35))];
        [button addTarget:self action:@selector(arriveAction) forControlEvents:UIControlEventTouchUpInside];
        button;
    });
    self.arriveBtn.hidden = YES;
    [self.bottomView addSubview:self.arriveBtn];
    
    UILabel * arriveLabel = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(22), ActualW(44), ActualH(13))];
        label.textColor = RGB(43, 142, 69);
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"确认送达";
        label.font = FontX(FONTSIZE(10));
        label;
    });
    
    [self.arriveBtn addSubview:arriveLabel];
    
    UIImageView * arriveImage = ({
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(ActualW(14), ActualH(0), ActualW(17), ActualH(17))];
        imageView.image = [UIImage imageNamed:@"arrive"];
        imageView;
    });
    [self.arriveBtn addSubview:arriveImage];
    
}





#pragma mark - -------------------------点击事件----------------------------
- (void)locationAction {
    //    [CustomAlert showAlertWithController:self andTitle:@"定位未开启" andMessage:nil andTime:1];
}
- (void)mobileAction {
    if (self.telCallBack) {
        self.telCallBack();
    }
}

- (void)arriveAction {
    if (self.arriveCallBack) {
        self.arriveCallBack();
    }
}

- (void)cancelAction {
    if (self.cancelCallBack) {
        self.cancelCallBack();
    }
}


#pragma mark - -------------------------数据部分----------------------------
- (void)reloadDataWithModel:(OrderFinishedModel *)model {
    //每次切换订单状态要修改 商品列表 的位置
    self.tableView.contentOffset = CGPointMake(0, 0);
    
    self.countLabel.text = [NSString stringWithFormat:@"#%@",model.sort];
    if ([model.order_stauts_text isEqualToString:@"已发货"]) {
        self.arriveBtn.hidden = NO;
        self.cancelBtn.hidden = NO;
        self.statusLabel.text = @"已接单";
    }else if ([model.order_stauts_text isEqualToString:@"已付款"]) {
        self.arriveBtn.hidden = YES;
        self.cancelBtn.hidden = YES;
        self.statusLabel.text = @"等待接单";
    }else {
        self.arriveBtn.hidden = YES;
        self.cancelBtn.hidden = YES;
        self.statusLabel.text = model.order_stauts_text;
    }
    self.name.text = model.order_receiver_name;
    self.buyCountLabel.text = [NSString stringWithFormat:@"第%@次下单",model.buy_second];
    self.addressLabel.text = model.order_receiver_address;
    self.goodsCount.text = [NSString stringWithFormat:@"商品 (%@)", model.list_count];
    self.receivePrice.text = [NSString stringWithFormat:@"￥%@", model.order_goods_amount];
    self.orderTime.text = [NSString stringWithFormat:@"订单时间：%@", model.order_create_time];
    self.orderNum.text = [NSString stringWithFormat:@"订单号：%@", model.order_id];
    
    [self.dataSource removeAllObjects];
    
    for (GoodsRowsModel * rowModel in model.goods_list) {
        [self.dataSource addObject:rowModel];
    }
    
    //小于五行禁止滑动
//    self.tableView.scrollEnabled = self.dataSource.count <= 5 ? NO : YES;
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paraStyle.alignment = NSTextAlignmentLeft;
    paraStyle.lineSpacing = 5; //设置行间距
    paraStyle.hyphenationFactor = 1.0;
    paraStyle.firstLineHeadIndent = 0.0;
    paraStyle.paragraphSpacingBefore = 0.0;
    paraStyle.headIndent = 0;
    paraStyle.tailIndent = 0;
    //设置字间距 NSKernAttributeName:@1.5f
    NSString * string = model.order_message;
    NSDictionary *dic = @{NSFontAttributeName:FontX(FONTSIZE(12)), NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@0.25f
                          };
    NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:string attributes:dic];
    self.remarkLabel.attributedText = attributeStr;
    CGSize size = [self.remarkLabel sizeThatFits:CGSizeMake(self.remarkLabel.frame.size.width, MAXFLOAT)];
    self.remarkLabel.frame = CGRectMake(self.remarkLabel.frame.origin.x, self.remarkLabel.frame.origin.y, size.width,size.height);
    
    [self.tableView reloadData];
    
}



#pragma mark - -------------------------协议代理部分----------------------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GoodsRowsTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[GoodsRowsTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    [cell reloadDataWithModel:self.dataSource[indexPath.row]];
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return ActualH(22);
}


#pragma mark - -------------------------懒加载----------------------------
- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = @[].mutableCopy;
    }
    return _dataSource;
}

@end
